package cn.idongjia.kuaidi.service.impl;

import cn.idongjia.kuaidi.common.MongoUtils;
import cn.idongjia.kuaidi.mapper.ShipMapper;
import cn.idongjia.kuaidi.pojos.Response;
import cn.idongjia.kuaidi.repository.KdRepo;
import cn.idongjia.kuaidi.service.ShipService;
import cn.idongjia.kuaidi.util.Utils;
import cn.idongjia.log.Log;
import cn.idongjia.log.LogFactory;
import cn.idongjia.tianji.pojos.ShipCompany;
import cn.idongjia.tianji.pojos.Shipment;
import cn.idongjia.tianji.query.ShipSearch;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongjia_lj on 16/4/18.
 */
@Path("shipments")
@Produces({"application/json;charset=UTF-8"})
public class ShipServiceImpl implements ShipService {
    private static final Log LOGGER = LogFactory.getLog(ShipServiceImpl.class);
    @Autowired
    ShipMapper shipMapper;
    @Autowired
    KdRepo kdRepo;

    @Override
    @POST
    @Consumes({"application/json;charset=UTF-8"})
    public Response add(Shipment shipment) {
        Response response = new Response(500);
        try {
            shipment.setCreatetm(Utils.getCurrentSecond());
            shipMapper.addShip(shipment);
            response.setRes(shipment);
            response.setCode(0);

            ShipCompany shipCompany = shipMapper.getCompany(Long.valueOf(shipment.getCid()));
            kdRepo.subScribe(shipment.getOoid(), shipment.getNo(), shipCompany.getName());

        } catch (Exception e) {
            response.setMsg(e.getMessage());
            LOGGER.error(e.getMessage(), e);
        }
        return response;
    }

    @Override
    @PUT
    @Path("{id}")
    @Consumes({"application/json;charset=UTF-8"})
    public Response update(@PathParam("id") String id, Shipment shipment) {
        Response response = new Response(500);
        try {
            shipment.setOoid(id);
            shipMapper.update(shipment);
            response.setCode(0);
            if(null !=shipment.getCid() || null !=shipment.getNo()) {
                kdRepo.update(shipment);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            response.setMsg(e.getMessage());
        }
        return response;
    }

    @Override
    @GET
    @Path("{id}")
    public Response getShipment(@PathParam("id") String id) {
        Response response = new Response(500);
        try {
            Shipment shipment = shipMapper.get(id);
            response.setRes(shipment);
            response.setCode(0);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            response.setMsg(e.getMessage());
        }
        return response;
    }

    @Override
    @GET
    public Response getAll(@BeanParam ShipSearch shipSearch) {
        Response response = new Response(500);
        try {
            List<Shipment> shipments = shipMapper.getAll(shipSearch);
            response.setRes(shipments);
            response.setCode(0);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            response.setMsg(e.getMessage());
        }
        return response;
    }

    @Override
    @GET
    @Path("count")
    public Response getCount(@BeanParam ShipSearch search) {

        Response response = new Response(500);
        try {
            int count = shipMapper.count(search);
            response.setRes(count);
            response.setCode(0);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            response.setMsg(e.getMessage());
        }
        return response;
    }

    @Override
    @DELETE
    @Path("{id}")
    public Response delete(@PathParam("id") String cid) {
        Response response = new Response(500);
        try {
            shipMapper.delete(cid);
            response.setCode(0);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            response.setMsg(e.getMessage());
        }
        return response;
    }

    @Override
    @POST
    @Path("companies")
    @Consumes({"application/json;charset=UTF-8"})
    public Response addCompany(ShipCompany shipCompany) {
        Response res = new Response(500);
        try {
            shipCompany.setCreatetm(Utils.getCurrentSecond());
            shipMapper.insertCompany(shipCompany);
            res.setCode(0);
            res.setRes(shipCompany);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            res.setMsg(e.getMessage());
        }
        return res;
    }

    @Override
    @DELETE
    @Path("companies/{id:\\d+}")
    public Response deleteCompany(@PathParam("id") Long id) {
        Response res = new Response(500);
        try {
            shipMapper.deleteCompany(id);
            res.setCode(0);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            res.setMsg(e.getMessage());
        }
        return res;
    }

    @Override
    @GET
    @Path("companies")
    public Response getCompanies(@BeanParam ShipSearch shipSearch) {
        Response res = new Response(500);
        try {
            List<ShipCompany> shipCompanies = shipMapper.getCompanies(shipSearch);
            res.setRes(shipCompanies);
            res.setCode(0);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            res.setMsg(e.getMessage());
        }
        return res;
    }
}
